"""
@date: 2023/7/11
@author: March
@desc: test

### 2.提取ip相关信息
* 前置知识：json、requests
* 接口：https://ip.taobao.com/outGetIpInfo?accessKey=alibaba-inc&ip=要查询的ip地址
* 给一个ip地址，可以通过该接口获取到省份和运营商
* 实现一个ip查询功能
    * 用户输入一个ip地址，将查询到的结果显示给用户
    * 并将结果用以下格式写入到文件：ip,国家,省份,运营商

"""
import requests
import json

url = 'https://ip.taobao.com/outGetIpInfo?accessKey=alibaba-inc&ip=175.0.151.214'
ip = "175.0.151.214"
f = open("./ip.txt", "a", encoding="utf-8")
try:
    resp = requests.get(url=url)
    resp.text
    str_data = resp.text
    data = json.loads(str_data)
    # print(data['data'])
    data1 = data['data']
    str1 = [data1['country'], data1['region'], data1['city'], data1['isp']]
    f.write(ip)
    f.write("，")
    for i in range(2):
        f.write(str1[i])
        f.write("，")
    f.write(str1[3])
except:
    print("出错了")
finally:
    print("关闭文件")
    f.close()

